<template>
  <div>
    <div style="float:left">
     <Tree :data="baseData" @on-select-change="getCheckedNodes" ></Tree>
    </div>
    <div style="float:right;width:80%">
       <Button type="primary" @click="addData">添加评论</Button>
        <Button type="primary" @click="alldeletd">删除选中</Button>
        <Input v-model="filter.content">
              <Button slot="append" icon="ios-search" @click="search"></Button>
        </Input>
        <Table border ref="selection"  @on-selection-change="allNode" :columns="columns1" :data="list"></Table>
            <Page :total="filter.total" show-elevator show-total size="small"  show-sizer :page-size-opts="[2,4,8,10]" @on-page-size-change="changePageSize" :page-size="2" @on-change="changePage" ></Page>
    </div>
    <Modal
        v-model="modal1"
        title="普通的Modal对话框标题"
        @on-ok="ok"
        @on-cancel="cancel">
        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <FormItem label="内容" prop="content">
            <quill-editor v-model="formValidate.content"
                ref="myQuillEditor"></quill-editor>
        </FormItem>
        
        
        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')" to='/user'>提交</Button>
            <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
        </FormItem>
    </Form>
    </Modal>

 </div>
</template>
<script>
import { quillEditor } from 'vue-quill-editor'
import base from '../.././common/base1'
    export default {
        mixins:[base],
        data () {
            return {
                baseData: [],
                module:'comment',
                columns1: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '内容',
                        key: 'content'
                    },
                    
                   
                    {
                        title: '时间',
                        key: 'date'
                    },
                    
                    {
                        title: '操作',
                        key: 'action',
                        fixed: 'right',
                        width: 120,
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'text',
                                        size: 'small'
                                    },
                                    on:{
                                      click:()=>{
                                        this.deleteData(params.row._id);
                                      }
                                    }
                                }, '删除'),
                                h('Button', {
                                    props: {
                                        type: 'text',
                                        size: 'small'
                                    },
                                    on:{
                                      click:()=>{
                                        this.editData(params.row._id);
                                        console.log(params.row._id);
                                      }
                                    }
                                }, '编辑')
                            ]);
                        }
                    }
                ],
                id:'',
                formValidate: {
                    content:'',
                   
                },
                ruleValidate: {
                    content: [
                        { required: true, message: '内容不能为空', trigger: 'change' }
                    ]
                }
            }
        },
        methods:{
            getCheckedNodes(arr){
                this.formValidate.cateId=arr[0]._id;
                this.$http({
                  method:'POST',
                  url: `http://localhost:3000/comment/list`,
                  data:{cateId:this.formValidate.cateId}
                }).then(res=>{
                  console.log(res.data);
                  this.list=res.data.rows;
                  this.filter.total=res.data.total;
                  console.log(res.data.rows);
                })
   
            },
            getTree(type){
                this.formValidate.type=type;
                this.$http({
                    method:'GET',
                    url:'http://localhost:3000/cate/list/'+type
                }).then((response) => {
                    this.baseData=response.data;
                })
            },
            ok () {
              this.getData();
              this.$Message.info('点击了确定');
          },
          cancel () {
            this.getData();
            this.$Message.info('点击了取消');
          },
          // handleSubmit (name) {
          //     this.$refs[name].validate((valid) => {
          //         if (valid) {
          //              var url='';
          //              if(this.id){
          //                 url='http://localhost:3000/comment/data/'+this.id;
          //              }else{
          //               url='http://localhost:3000/comment/data';
          //               // this.formValidate.newsId=getCookie('newsId');
          //               // this.formValidate.userId=getCookie('userId');
          //                newsId:this.$route.params.id
          //              }
          //              this.$http({
          //                 method:'POST',
          //                 url: url,
          //                 data:this.formValidate
          //               }).then(response=>{
          //                 console.log(response.data);
          //               })
          //             this.$Message.success('提交成功!');
          //         } else {
          //             this.$Message.error('表单验证失败!');
          //         }
          //     })
          // },
          handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        if(this.formValidate._id && this.formValidate._id.length>0){
                            this.$http.post(`http://localhost:3000/comment/data/${this.formValidate._id}`,this.formValidate)
                            .then(res=>{
                                this.$Message.success('提交成功!');
                                this.modal = false;
                                this.getData();
                            })
                        }else{
                            /*this.$http.post(`http://localhost:3000/comment/data`,this.formValidate)
                            .then(res=>{
                                this.$Message.success('提交成功!');
                                this.modal = false;
                                this.getData();

                            })*/
                            this.$http({
                                    url:'http://localhost:3000/comment/data',
                                    method:'POST',
                                    data:{
                                        author:this.formValidate.author,
                                        title:this.formValidate.title,
                                        content:this.formValidate.content,
                                        newsId:this.$route.params.id,
                                        cateId:this.formValidate.cateId
                                    }
                                })
                            //this.$http.post('http://localhost:3000/comment/data',this.formValidate)
                            .then(res=>{
                                this.$Message.success('提交成功!');
                                this.modal=false;
                                this.getData();
                            })
                        }
                        
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
          handleReset (name) {
              this.$refs[name].resetFields();
          }
        },
        components: {
            quillEditor
          },
        created(){
          this.filter.newsId=this.$route.params.id;
          this.getTree(1);
        }
    }
</script>